package rahcir;

import java.io.IOException;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.*;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.api.datastore.Text;
import com.google.appengine.api.datastore.Link;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

@SuppressWarnings("serial")
public class RahcirServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws IOException {
		doPost(req, resp);
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		/// LOGIN ////
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        if (user == null) {
            resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
            return;
        }

        /// Request Dispache ///
		String type = req.getParameter("type");
		if ("menu".equals(type)) {
			resp.sendRedirect("menu.jsp");
			
//		} else if ("registShopForm".equals(type)) {
//			resp.sendRedirect("registShopForm.jsp");	
//		} else if ("editShopForm".equals(type)) {
//			resp.sendRedirect("editShopForm.jsp");			
//		} else if ("infoShop".equals(type)) {
//			resp.sendRedirect("infoShop.jsp");	
//		} else if ("listShop".equals(type)) {
//			resp.sendRedirect("listShop.jsp");	
//			
//		} else if ("registEatForm".equals(type)) {
//			resp.sendRedirect("registEatForm.jsp");	
//		} else if ("editEatForm".equals(type)) {
//			resp.sendRedirect("editEatForm.jsp");			
//		} else if ("infoEat".equals(type)) {
//			resp.sendRedirect("infoEat.jsp");	
//		} else if ("listEat".equals(type)) {
//			resp.sendRedirect("listEat.jsp");	
//			
//		} else if ("registAssembleForm".equals(type)) {
//			resp.sendRedirect("registAssembleForm.jsp");	
//		} else if ("editAssembleForm".equals(type)) {
//			resp.sendRedirect("editAssembleForm.jsp");			
//		} else if ("infoAssemble".equals(type)) {
//			resp.sendRedirect("infoAssemble.jsp");	
//		} else if ("listAssemble".equals(type)) {
//			resp.sendRedirect("listAssemble.jsp");	

			// Store Data
		} else if ("editShop".equals(type)) {
			editShop(req, resp);
		} else if ("registShop".equals(type)) {
			registShop(req, resp);
		} else if ("deleteShop".equals(type)) {
			deleteShop(req, resp);
			
		} else if ("editEat".equals(type)) {
			editEat(req, resp);		
		} else if ("registEat".equals(type)) {
			registEat(req, resp);	
		} else if ("deleteEat".equals(type)) {
			deleteEat(req, resp);
			
		} else if ("editAssemble".equals(type)) {
			editAssemble(req, resp);	
		} else if ("registAssemble".equals(type)) {
			registAssemble(req, resp);	
		} else if ("deleteAssemble".equals(type)) {
			deleteAssemble(req, resp);	
			
			
		} else {
			resp.sendRedirect("menu.jsp");
		}
	}
	
	private void editShop(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String id = req.getParameter("id");
        
        String shopname = req.getParameter("shopname");
        String comment = req.getParameter("comment");
        String address = req.getParameter("address");
        String mapurl = req.getParameter("mapurl");

        PersistenceManager pm = PMF.get().getPersistenceManager();
        pm.currentTransaction().begin();
        try {
        	Shop shop = pm.getObjectById(Shop.class, Integer.parseInt(id));
        	
        	shop.setShopname(shopname);
        	shop.setComment(new Text(comment));
        	shop.setAddress(address);
        	shop.setMapurl(new Link(mapurl));

            pm.currentTransaction().commit();
        } catch (Throwable t) {
        	// rollback in case of errors
        	pm.currentTransaction().rollback();
        	t.printStackTrace();
        } finally {
            pm.close();
        }

        resp.sendRedirect("listShop.jsp");
	}
	
	private void registShop(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String shopname = req.getParameter("shopname");
        String comment = req.getParameter("comment");
        String address = req.getParameter("address");
        String mapurl = req.getParameter("mapurl");

        PersistenceManager pm = PMF.get().getPersistenceManager();
        pm.currentTransaction().begin();
        
        try {
            Shop shop = new Shop(shopname, new Text(comment), address, new Link(mapurl));
            pm.makePersistent(shop);
            pm.currentTransaction().commit();
        } catch (Throwable t) {
        	// rollback in case of errors
        	pm.currentTransaction().rollback();
        	t.printStackTrace();
        } finally {
            pm.close();
        }
        
        resp.sendRedirect("listShop.jsp");		
	}
	
	private void deleteShop(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String id = req.getParameter("id");
        
        PersistenceManager pm = PMF.get().getPersistenceManager();
        pm.currentTransaction().begin();
        try {
        	Shop shop = pm.getObjectById(Shop.class, Integer.parseInt(id));
        	pm.deletePersistent(shop);
            pm.currentTransaction().commit();
        } catch (Throwable t) {
        	// rollback in case of errors
        	pm.currentTransaction().rollback();
        	t.printStackTrace();
        } finally {
            pm.close();
        }

        resp.sendRedirect("listShop.jsp");
	}
	
	private void editEat(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
	}	
	private void registEat(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
	}
	private void deleteEat(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
	}	
	private void editAssemble(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
	}
	private void registAssemble(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
	}
	private void deleteAssemble(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
	}
	
	
}
